﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using BusinessObjects.BO_GetObjects;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using DigiLib;

/// <summary>
/// Summary description for Base_Controls
/// </summary>
public class Base_Controls : System.Web.UI.UserControl
{
    public int Mid { get; set; }
    public string MName { get; set; }
    public string ContentName { get; set; }
    private int cur_page;
    public int Cur_page
    {
        get
        {
            cur_page = getRequest("cur_page", 1);
            return cur_page;
        }
        set { cur_page = value; }
    }
    public int Cid { get; set; }    
    public int PSize { get; set; }
    public int SPage { get; set; }
    public int MType { get; set; }
    protected DataTable ObjDT { get; set; }
    public Base_Controls()
    {
    }
    protected void setField(int mid)
    {
        this.Mid = mid;
        ObjDT = BO_portal_modules.Search(mid);
        if (ObjDT != null && ObjDT.Rows.Count > 0)
        {
            MName = ObjDT.Rows[0]["FULL_NAME"].ToString();
            PSize = Convert.ToInt32(ObjDT.Rows[0]["PAGE_SIZE"]);
            Cid = Convert.ToInt32(ObjDT.Rows[0]["CATEGORY_ID"]);
            MType = Convert.ToInt32(ObjDT.Rows[0]["MODULE_TYPE_ID"]);
            ContentName = string.Empty;//ObjDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
        }
    }

    protected string getRequest(string req, string nullval)
    {
        string val;
        if (Request[req] != null)
            val = Request[req].ToString();
        else
            val = nullval;
        return val;
    }
    protected int getRequest(string req, int nullval)
    {
        int val;
        if (Request[req] != null)
            val = Convert.ToInt32(Request[req]);
        else
            val = nullval;
        return val;
    }
    protected string bindRepeater(Repeater rpt, DataTable objDT)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                rpt.DataSource = objDT;
                rpt.DataBind();
                return string.Empty;
            }
            else
            {
                rpt.DataSource = null;
                rpt.DataBind();
                return "Đang cập nhật dữ liệu...";
            }
        }
        catch { throw; }
    }
    protected bool bindRepeater(Repeater rpt, DataTable objDT, HtmlGenericControl pager)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                /////////           Pager       //////////                                
                string url = Request.Url.AbsoluteUri;
                if (url.IndexOf("&cur_page") > 0)
                {
                    string temp1, temp2;
                    temp1 = url.Remove(url.IndexOf("&cur_page"));
                    temp2 = url.Replace(temp1, "");
                    int index = temp2.IndexOf("&", 1);
                    if (index > 0)
                        temp2 = temp2.Substring(index, temp2.Length - index);
                    else
                        temp2 = "";
                    url = temp1 + temp2;
                }
                url += "&cur_page=$CUR_PAGE";
                pager.InnerHtml = DigiPager.Paging(PSize, Cur_page, Convert.ToInt32(objDT.Rows[0]["TOTAL_ROWS"]), url);
                pager.Visible = true;
                /////////           End Pager       ////////                                
                rpt.DataSource = objDT;
                rpt.DataBind();
                rpt.Visible = true;
                return true;
            }
            else
            {
                rpt.Visible = false;
                pager.InnerHtml = string.Empty;
                pager.Visible = false;
                rpt.DataSource = null;
                rpt.DataBind();
                return false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected bool bindRepeater(Repeater rpt, DataTable objDT, HtmlGenericControl pager, string iden)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                /////////           Pager       //////////                                
                string url = Request.Url.AbsoluteUri;
                if (url.IndexOf("&cur_page") > 0)
                {
                    string temp1, temp2;
                    temp1 = url.Remove(url.IndexOf("&cur_page"));
                    temp2 = url.Replace(temp1, "");
                    int index = temp2.IndexOf("&", 1);
                    if (index > 0)
                        temp2 = temp2.Substring(index, temp2.Length - index);
                    else
                        temp2 = "";
                    url = temp1 + temp2;
                }
                url += "&cur_page=$CUR_PAGE";
                pager.InnerHtml = DigiPager.Paging(PSize, Cur_page, Convert.ToInt32(objDT.Rows[0]["TOTAL_ROWS"]), url);
                pager.Visible = true;
                /////////           End Pager       ////////                                
                rpt.DataSource = objDT;
                rpt.DataBind();
                rpt.Visible = true;
                return true;
            }
            else
            {
                rpt.Visible = false;
                pager.InnerHtml = string.Empty;
                pager.Visible = false;
                rpt.DataSource = null;
                rpt.DataBind();
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
            throw ex;
        }
    }
    protected string GetSrc(string src)
    {
        string extension = Path.GetExtension(src);
        if (extension == ".swf")
            src = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"../upload/ads/" + src + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"../upload/ads/" + src + "\"></object>";
        else src = "/upload/" + src;
        return src;
    }

    private bool lang;
    public bool Lang
    {
        get { if (Session["lang"] != null && Session["lang"].ToString().Equals("en-US")) return true; else return false; }
    }
}